package com.ltxtiyu.video.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ltxtiyu.video.service.ms.UserService;
import com.ltxtiyu.video.tool.StringUtil;
import com.ltxtiyu.video.vo.ms.AddressVO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@RequestMapping("userController")
@Controller
public class UserController {
    @Reference
    private UserService userService;

    /**
     * 条件查询列表
     * @param page
     * @param limit
     * @param name
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value = "paramlist",method = RequestMethod.GET)
    public ResponseEntity<Map<String ,Object>> paramlist(int page, int limit, String name, String startTime,String endTime){
        try {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

            Map<String,Object> map = new HashMap<>();
            map.put("page",(page-1)*limit);
            map.put("row",limit);
            if(StringUtil.isNotBlank(name)){

                map.put("name",name);
            }
            if(StringUtil.isNotBlank(startTime)){

                map.put("startTime",format.parse(startTime));
            }
            if(StringUtil.isNotBlank(endTime)){

                map.put("endTime",format.parse(endTime));
            }
            Map<String,Object> result = userService.selectParamList(map);

            return ResponseEntity.ok(result);
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }

    }

    /**
     * 封印
     * @param id
     * @return
     */
    @RequestMapping(value = "sealUser")
    public ResponseEntity<Void> sealUser(Integer id){
        try {
            if (id.longValue() <1){
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            userService.sealUser(id);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }


    }

    /**
     * 解封
     * @param id
     * @return
     */
    @RequestMapping(value = "unSealUser")
    public ResponseEntity<Void> unSealUser(Integer id){
        try {
            if (id.longValue() <1){

                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            userService.unSealUser(id);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }


    }

    /**
     * 用户分布
     * @return
     */
    @RequestMapping(value = "userAddress")
    public ResponseEntity userAddress(){
        try {
           List<AddressVO> addressList=userService.userAddress();
            return new ResponseEntity(addressList,HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
